// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hardware_code:"",
    orders:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  scan(){
    const self = this;
    wx.scanCode().then((res) =>{
      const hardware_code = res.result;
      self.setData({
        hardware_code:hardware_code
      })
      wx.showLoading({
        title:"数据加载中。。。"
      })
      self.query();
    }).catch(err => {
      console.log(err);
    });
  },
  call_material(e){
    const target = e.target;
    console.log(target);
    const mac = target.dataset.mac;
    const num = target.dataset.num;
    const self = this;
    wx.request({
      url: 'https://logisticsengineering.gree.com/order_system/get_push_result',
      data:{
        mac:mac,
        result:num
      },
      success(res){
        const result = res.data;
        const code = result["code"];
        const data = result["data"];
        if(code==20000){
          wx.showToast({
            title: '叫料成功',
          })
          self.query();
        }else{
          wx.showToast({
            title:result.msg,
            icon:"error"
          })
        }
      }
    })
  },
  cancel_material(e){
    const target = e.target;
    console.log(target);
    const request_order = target.dataset.request_order;
    const operation = target.dataset.operation;
    const job_name = target.dataset.job_name;
    const self = this;
    console.log(request_order,operation,job_name);
    wx.request({
      url: 'https://logisticsengineering.gree.com/order_system/cancel_call_m',
      method:"POST",
      data:{
        request_order,
        operation,
        job_name
      },
      success(res){
        const data = res.data;
        console.log(data);
        const code = data.code;
        if(code == 20000){
          wx.showToast({
            title: '取消成功',
          })
          self.query();
        }else{
          wx.showToast({
            title: data.msg,
          })
        }
        
      }
    })
  },
  query(){
    console.log("xxx");
    const self = this;
    wx.request({
      url: 'https://logisticsengineering.gree.com/order_system/query_hardware_info',
      method:"POST",
      data:{
        hardware_code:this.data.hardware_code
      },
      success(res){
        wx.hideLoading();
        const result = res.data;
        const code = result["code"];
        const data = result["data"];
        console.log(data);
        if(code==20000){
          self.setData({
            orders:data
          });
        }else{
          wx.showToast({
            title: result.msg,
            icon:"error"
          })
          self.setData({
            orders:[]
          })
        }
      },
      fail(){
        wx.hideLoading();
      }
    });
  }
})